package shiroDemo;

import org.apache.shiro.authc.*;
import org.apache.shiro.realm.Realm;

public class MyRealm implements Realm {

    @Override
    public String getName() {
        return "myRealm";
    }

    @Override
    public boolean supports(AuthenticationToken token) {
        return token instanceof UsernamePasswordToken;
    }

    @Override
    public AuthenticationInfo getAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        String userName = (String) token.getPrincipal();
        /** {@link org.apache.shiro.authc.UsernamePasswordToken#getPassword }**/
        String password = new String((char[])token.getCredentials());

        if(!"admin".equals(userName)){
            throw new UnknownAccountException("userName 不存在！");
        }

        if(!"123456".equals(password)){
            throw new IncorrectCredentialsException("password 不正确！");
        }
        return new SimpleAuthenticationInfo(userName,password,getName());
    }
}
